<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>生成license</title>
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="stylesheet" type="text/css" href="css/reset.css">
    <script type="text/javascript" src="js/rem.js"></script>
    <script type="text/javascript" src="js/jquery-3.2.1.js"></script>
    <script type="text/javascript" src="js/layui/layui.js"></script>
</head>
<body>
<div class="generateLicense">
    <div class="generateLicense_content">
        <div class="license_title">生成license</div>
        <div>
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <span style="color: red">*</span>
                    <label class="layui-form-label">license授权有效日期</label>
                    <div class="layui-inline" id="test-range">
                      <div class="layui-input-inline">
                        <input type="text" id="startDate" class="layui-input" placeholder="开始日期" name="notBeforeTime">
                      </div>
                      <div class="layui-form-mid">至</div>
                      <div class="layui-input-inline">
                        <input type="text" id="endDate" class="layui-input" placeholder="结束日期" name="notAfterTime">
                      </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">Mac地址</label>
                    <div class="layui-input-block">
                      <input type="text" name="macAddress" placeholder="请Mac地址" autocomplete="off" class="layui-input">
                    </div>
                    <div class="tips">支持多个Mac地址，用“ ; ”隔开</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">IP地址</label>
                    <div class="layui-input-block">
                      <input type="text" name="ipAddress" placeholder="请IP地址" autocomplete="off" class="layui-input">
                    </div>
                    <div class="tips">支持多个IP地址，用“ ; ”隔开</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户数</label>
                    <div class="layui-input-block">
                      <input type="text" name="userNum" placeholder="请输入用户数" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户数</label>
                    <div class="layui-input-block">
                      <input type="text" name="clientNum" placeholder="请输入客户数" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-itemBtn">
                    <div class="layui-input-block">
                      <button  id="create" class="layui-btn" lay-submit lay-filter="formDemo">创建并下载</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>

    var ctxPath = "http://www.kangxijiankang.com/bcps";

    // 获取当前日期
    var myDate = new Date();
    let currentDate = myDate.getFullYear() + '-' + (myDate.getMonth()+1) + '-' + myDate.getDate();
    // 定义搜索条件的值
    let selectDate = currentDate,contentdata = '-1';

    layui.use(['form','laydate'], function(){
        var form = layui.form,laydate = layui.laydate;
        laydate.render({
            elem: '#test-range',
            range: ['#startDate', '#endDate'],
        });
        // 监听提交
        form.on('submit(formDemo)', function(data){
            let info=data.field
            if(!info.notBeforeTime||!info.notAfterTime){
                layer.msg("请填写有效日期");
                return false
            }
            let var1=new Date(info.notBeforeTime).getTime()
            let var2=new Date(info.notAfterTime).getTime()
            if(var1>var2){
                layer.msg("开始日期不能大于结束日期");
                return false
            }
            let intVerify=/^[1-9]\d*$/
            if(info.userNum&&!intVerify.test(info.userNum)){
                layer.msg("用户数请填写正整数");
                return false
            }
            if(info.clientNum&&!intVerify.test(info.clientNum)){
                layer.msg("客户数请填写正整数");
                return false
            }
            let macAddress=[],ipAddress=[]
            if(info.macAddress){
                macAddress=info.macAddress.split(";")
            }
            if(info.ipAddress){
                ipAddress=info.ipAddress.split(";")
            }
            let param={
                notBeforeTime:info.notBeforeTime,
                notAfterTime:info.notAfterTime,
                licenseCheckModel:{
                    macAddress:macAddress,
                    ipAddress:ipAddress,
                    userNum:info.userNum,
                    clientNum:info.clientNum
                },
            }
                $("#create").attr("disabled",true)
                $.ajax({
                    type: "post",
                    url: "/api/license/create",
                    contentType:"application/json",
                    data:JSON.stringify(param),
                    dataType:"json",
                    success:function(d){
                        layer.msg(d.message);
                        if(d.result="success"){
                            download("/api/license/download","file","get")
                        }
                    }
                });
            return false
        });
    });
    function download(url,name,method){
            //拼接一个form表单  里面有input标签  追加到<body>标签中 提交后删除这个form表单；
            $('<form action="'+url+'" method="'+method+'"><input type="text" name="'+name+'"/></form>')
                .appendTo("body").submit().remove();
    }
</script>
</body>
</html>
